package com.fancylargo.crowd.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.fancylargo.crowd.common.Result.Result;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fancylargo.crowd.entity.CategoryEntity;
import com.fancylargo.crowd.service.CategoryService;
import com.fancylargo.crowd.common.utils.PageUtils;


/**
 * 商品三级分类
 *
 * @author chaiguoqiang
 * @email 15532050839@163.com
 * @date 2020-06-28 14:44:47
 */
@RestController
@RequestMapping("product/crowd/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出商品分类列表
     */
    @RequestMapping("/list/catalog")
    //@RequiresPermissions("crowd:category:list")
    public Result list() {
        List<CategoryEntity> list = categoryService.listWithTree();

        return Result.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("crowd:category:info")
    public Result info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return Result.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("crowd:category:save")
    public Result save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("crowd:category:update")
    public Result update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);

        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("crowd:category:delete")
    public Result delete(@RequestBody Long[] catIds) {

        // 检查当前删除节点是否被别的地方引用
        categoryService.removeByIds(Arrays.asList(catIds));

        return Result.ok();
    }

}
